package cn.elwy.rcp.ui.explorer.core.dao.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.configuration.tree.ConfigurationNode;
import org.springframework.stereotype.Repository;

import cn.elwy.common.exception.ConfigException;
import cn.elwy.common.i18n.Msg;
import cn.elwy.common.util.ReflectUtil;
import cn.elwy.osgi.config.ConfigContext;
import cn.elwy.rcp.ui.explorer.core.dao.BuilderModuleConfigDao;

@Repository(BuilderModuleConfigDao.ID)
public class BuilderModuleConfigDaoImpl implements BuilderModuleConfigDao {

	/**
	 * 加载工程配置文件
	 * @param projectPath 配置文件路径
	 * @return
	 */
	public Map<String, String> load(File file) {
		try {
			return load(new FileInputStream(file), ConfigContext.DEFAULT_ENCODING);
		} catch (Exception e) {
			throw new ConfigException(Msg.E_LOAD_CONFIG_ERROR, e);
		}
	}

	/**
	 * 加载工程配置文件
	 * @param file 配置文件路径
	 * @return
	 */
	public Map<String, String> load(URL url) {
		try {
			return load(url.openStream(), ConfigContext.DEFAULT_ENCODING);
		} catch (IOException e) {
			throw new ConfigException(Msg.E_LOAD_CONFIG_ERROR, e);
		}
	}

	/**
	 * 加载工程配置文件
	 * @param file 配置文件路径
	 * @return
	 */
	public Map<String, String> load(InputStream in, String encoding) {
		XMLConfiguration config = new XMLConfiguration();
		try {
			config.load(in);
			config.setEncoding(encoding);
		} catch (ConfigurationException e) {
			throw new ConfigException(Msg.E_LOAD_CONFIG_ERROR, e);
		} finally {
			ReflectUtil.close(in);
		}
		Map<String, String> map = new HashMap<String, String>();
		ConfigurationNode rootNode = config.getRootNode();
		int childrenList = rootNode.getChildrenCount();
		for (int i = 0; i < childrenList; i++) {
			String value = config.getString("module(" + i + ")[@outDirectory]");
			int childrenCount = rootNode.getChild(i).getChildrenCount();
			for (int j = 0; j < childrenCount; j++) {
				String key = config.getString("module(" + i + ").item(" + j + ")");
				map.put(key, value);
			}
		}
		return map;
	}

	public void save(Map<String, String> map, File file) {

		XMLConfiguration config = new XMLConfiguration();
		ConfigurationNode rootNode = config.getRootNode();
		int childrenList = rootNode.getChildrenCount();
		Set<Entry<String, String>> entrySet = map.entrySet();

		Set<String> values = new HashSet<String>();
		int i = 0;
		for (Entry entry : entrySet) {
			String value = config.getString("module(" + i + ")[@outDirectory]");
			List<ConfigurationNode> items = rootNode.getChildren("module(" + i + ").item");
			for (int j = 0; j < items.size(); j++) {
				String key = config.getString("module(" + i + ").item(" + j + ")");
				map.put(key, value);
			}
		}

		try {
			file.getParentFile().mkdirs();
			config.save(file);
		} catch (ConfigurationException e) {
			throw new ConfigException(e.getMessage(), e);
		}
	}

}
